﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using BasicModuleOrm.Models;
using Microsoft.EntityFrameworkCore;
using BasicModuleOrm.Db_Context;
using BasicModuleOrm.Model;
using System.Collections.Immutable;
using Microsoft.EntityFrameworkCore.Internal;

namespace BasicModuleOrm.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ProductController : ControllerBase
    {
        public EfCoreContext _efCoreContext { get; set; }

        public ProductController(EfCoreContext efCoreContext)
        {
            this._efCoreContext = efCoreContext;
        }

        /// <summary>
        /// 查询所有商品
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult<IEnumerable<Product>>> GetProducts()
        {
            return await _efCoreContext.Product.ToListAsync();
        }

        /// <summary>
        /// 查询所有商品
        /// </summary>
        /// <returns></returns>
        [HttpGet("ProductImage")]
        public async Task<ActionResult<IEnumerable<Product>>> GetProductImages()
        {
            return await _efCoreContext.Product.
                            Include(p => p.productImages). // 加载导航属性
                            ToListAsync();
        }

        //[HttpGet("PermissionList")]
        //public async Task<List<RolePermission>> GetRolePermissionList()
        //{
        //    List<RolePermission> rolePermissions = await appDBContext.RolePermissions.ToListAsync();
        //    return rolePermissions;
        //}

        /// <summary>
        /// 查询商品图片根据商品Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}/ProductImage")]
        public async Task<ActionResult<Product>> GetProductImage(int id)
        {
            //// 1、查询商品
            var product = await _efCoreContext.Product.
                                    Include(p => p.productImages).// 加载导航属性
                                                                  //ThenInclude(up => up.Url). //嵌套导航属性
                                    Where(p => p.Id == id).
                                    FirstAsync(); 

            if (product == null)
            {
                return NotFound();
            }

            return product;
        }

        /// <summary>
        /// 查询商品根据商品Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ActionResult<Product>> GetProduct(int id)
        {
            // 1、查询商品
            var product = await _efCoreContext.Product.FindAsync(id);

            if (product == null)
            {
                return NotFound();
            }

            return product;
        }

        ///// <summary>
        ///// 查询所有商品
        ///// </summary>
        ///// <returns></returns>
        //[HttpGet]
        //public async Task<ActionResult> GetProducts()
        //{
        //    List<Product> xx = new List<Product>(); xx.Take(1).ToList();
        //    //return await _efCoreContext.Product.ToListAsync();
        //    var data = from i in _efCoreContext.Product select i;
        //    return new JsonResult(data.ToList());
        //}

        /// <summary>
        /// 添加商品图片
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        [HttpPost("ProductImage")]
        public async Task<ActionResult<Product>> PostProductImage(Product product)
        {
            // 1、添加商品
            _efCoreContext.Product.Add(product);
            await _efCoreContext.SaveChangesAsync();

            return CreatedAtAction(nameof(PostProduct), new { id = product.Id }, product);
        }


        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> PostProduct(Product product)
        {
            // 1、添加商品
            _efCoreContext.Product.Add(product);
            await _efCoreContext.SaveChangesAsync();

            return new JsonResult(product);
        }

        /// <summary>
        /// 更新商品通过Id
        /// </summary>
        /// <param name="id"></param>
        /// <param name="product"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<IActionResult> PutProduct(int id, Product product)
        {
            //1、设置更新状态
            _efCoreContext.Entry(product).State = EntityState.Modified;

            try
            {
                //_efCoreContext.Product.Update(product);
                //2、开始更新
                await _efCoreContext.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }

            return Ok("更新成功");
        }

        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteProduct(int id)
        {
            var product = await _efCoreContext.Product.FindAsync(id);
            if (product == null)
            {
                return NotFound();
            }

            // 1、开始删除
            _efCoreContext.Product.Remove(product);
            await _efCoreContext.SaveChangesAsync();

            return Ok("删除成功");
        }
    }
}
